package com.lanyou.esb.cook.proxy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.entity.PfThroughput;

/**
 * 平台吞吐量持久层
 * 
 * @author Davey.wu
 */
public class PfThroughputDao extends BaseDao<PfThroughput> {
	private static final SimpleDateFormat DATEFORMATE = new SimpleDateFormat(
			Constants.DATE_PATTERN_YYYYMMDDHHMMSS);
	private static final List<String> fields = new ArrayList<String>();

	/** SQL **/
	private static final String SQL;

	static {
		fields.add("FIELD_ID");
		fields.add("STATISTICS_DATE");
		fields.add("THROUGHPUT_QTY");

		fields.add("IS_ENABLE");
		fields.add("CREATOR");
		fields.add("CREATED_DATE");
		fields.add("MODIFIER");
		fields.add("LAST_UPDATED_DATE");
	}
	static {
		StringBuilder builderFields = new StringBuilder();
		StringBuilder builderValues = new StringBuilder();
		builderFields.append(" (");
		builderValues.append(" (");
		int size = fields.size();
		for (int i = 0; i < size; ++i) {
			builderFields.append(fields.get(i) + (i == size - 1 ? "" : ","));
			builderValues.append((i == size - 1 ? "?" : "?,"));
		}
		builderFields.append(") ");
		builderValues.append(") ");
		SQL = "insert into T_ESB_BU_PF_THROUGHPUT" + builderFields.toString()
				+ "values " + builderValues.toString();
	}
	private RowMapper<PfThroughput> mapper = new RowMapper<PfThroughput>() {
		public PfThroughput mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			PfThroughput rcd = new PfThroughput();
			rcd.setFieldId(rs.getString("FIELD_ID"));
			rcd.setStatisticsDate(rs.getDate("STATISTICS_DATE"));
			rcd.setThroughputQty(rs.getDouble("THROUGHPUT_QTY"));
			rcd.setIsEnable(rs.getString("IS_ENABLE"));
			rcd.setCreator(rs.getString("CREATOR"));
			rcd.setCreateDate(rs.getDate("CREATED_DATE"));
			rcd.setModifier(rs.getString("MODIFIER"));
			rcd.setLastUpdatedDate(rs.getDate("LAST_UPDATED_DATE"));
			return rcd;
		}
	};

	@Override
	public int save(PfThroughput pfThroughput) {
		return getJdbcTemplate().update(SQL, pfThroughput.getFieldId(),
				getFormatedDate(pfThroughput.getStatisticsDate()),
				pfThroughput.getThroughputQty(), pfThroughput.getIsEnable(),
				pfThroughput.getCreator(), pfThroughput.getCreateDate(),
				pfThroughput.getModifier(), pfThroughput.getLastUpdatedDate());
	}

	@Override
	public int saveAll(List<PfThroughput> pfThroughputs) {
		if (pfThroughputs == null || pfThroughputs.isEmpty()) {
			return 0;
		}
		List<PfThroughput> saves = new ArrayList<>(pfThroughputs.size());
		List<PfThroughput> updates = new ArrayList<>(pfThroughputs.size());
		for (PfThroughput pfThroughput : pfThroughputs) {
			PfThroughput temp = this.find(pfThroughput);
			if (temp == null) {
				saves.add(pfThroughput);
				continue;
			}
			temp.setThroughputQty(pfThroughput.getThroughputQty());
			updates.add(temp);
		}

		int result = save(saves);
		result += update(updates);
		return result;
	}

	/**
	 * 保存平台吞吐量
	 * 
	 * @param pfThroughputs
	 * @return
	 */
	private int save(List<PfThroughput> pfThroughputs) {
		if (pfThroughputs == null || pfThroughputs.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>(pfThroughputs.size());
		int i = 0;
		for (PfThroughput pfThroughput : pfThroughputs) {
			i = 0;

			Object[] objects = new Object[fields.size()];
			objects[i++] = pfThroughput.getFieldId();
			objects[i++] = getFormatedDate(pfThroughput.getStatisticsDate());
			objects[i++] = pfThroughput.getThroughputQty();
			objects[i++] = pfThroughput.getIsEnable();
			objects[i++] = pfThroughput.getCreator();
			objects[i++] = pfThroughput.getCreateDate();
			objects[i++] = pfThroughput.getModifier();
			objects[i++] = pfThroughput.getLastUpdatedDate();
			args.add(objects);
		}
		int[] results = getJdbcTemplate().batchUpdate(SQL, args);
		if (results == null) {
			return 0;
		}
		int result = 0;
		for (int k : results) {
			result += k;
		}
		return result;
	}

	@Override
	public int update(List<PfThroughput> pfThroughputs) {
		if (pfThroughputs == null || pfThroughputs.isEmpty()) {
			return 0;
		}
		List<Object[]> args = new ArrayList<>(pfThroughputs.size());
		int i = 0;
		for (PfThroughput pfThroughput : pfThroughputs) {
			i = 0;
			Object[] objects = new Object[3];
			objects[i++] = pfThroughput.getThroughputQty();
			objects[i++] = pfThroughput.getFieldId();
			objects[i++] = getFormatedDate(pfThroughput.getStatisticsDate());
			args.add(objects);
		}
		String sql = "UPDATE T_ESB_BU_PF_THROUGHPUT SET THROUGHPUT_QTY=THROUGHPUT_QTY+? WHERE FIELD_ID=? AND STATISTICS_DATE=?";
		int[] results = getJdbcTemplate().batchUpdate(sql, args);
		if (results == null) {
			return 0;
		}
		int result = 0;
		for (int k : results) {
			result += k;
		}
		return result;
	}

	@Override
	public PfThroughput find(PfThroughput target) {
		if (target == null || target.getStatisticsDate() == null) {
			return null;
		}
		String date = getFormatedDateAsStr(target.getStatisticsDate());
		String sql = "SELECT * FROM T_ESB_BU_PF_THROUGHPUT t WHERE t.FIELD_ID=? AND t.STATISTICS_DATE=?";
		PfThroughput pfThroughput = null;
		try {
			pfThroughput = this.getJdbcTemplate().queryForObject(sql, mapper,
					target.getFieldId(), date);
		} catch (EmptyResultDataAccessException e) {
			pfThroughput = null;
		}

		return pfThroughput;
	}

	/**
	 * 消除时分秒毫秒
	 * 
	 * @param date
	 * @return
	 */
	private String getFormatedDateAsStr(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return DATEFORMATE.format(calendar.getTime());
	}

	/**
	 * 消除时分秒毫秒
	 * 
	 * @param date
	 * @return
	 */
	private Date getFormatedDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
}
